package cn.south.campus.admin.campus.service.impl;

import cn.south.campus.admin.campus.mapper.AdMapper;
import cn.south.campus.admin.campus.model.entity.Ad;
import cn.south.campus.admin.campus.model.req.AdReq;
import cn.south.campus.admin.campus.model.vo.AdVO;
import cn.south.campus.admin.campus.service.AdService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import cn.south.campus.common.utils.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-11-11
 */
@Service
@AllArgsConstructor
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    private final AdMapper adMapper;

    @Override
    public List<AdVO> listAd(AdReq req) {
        return adMapper.selectByList(req).stream().map(AdVO::convert).collect(Collectors.toList());
    }

    @Override
    public IPage<AdVO> pageAd(AdReq req) {
        return adMapper.selectByPage(req.getPageDto(), req).convert(AdVO::convert);
    }

    @Override
    public void saveAd(AdVO adVO) {
        adMapper.insert(AdVO.convert(adVO));
    }

    @Override
    public void updateAd(AdVO adVO) {
        Ad ad = adMapper.selectById(adVO.getId());
        AssertUtils.isNotNull(ad, CommonEnums.DATA_HAS_NOT_ERROR);
        ad.setPosNo(adVO.getPosNo());
        ad.setTitle(adVO.getTitle());
        ad.setContent(adVO.getContent());
        ad.setUrl(adVO.getUrl());
        ad.setStartTime(DateUtil.parseString(adVO.getStartTime()));
        ad.setEndTime(DateUtil.parseString(adVO.getEndTime()));
        adMapper.updateById(ad);
    }
}
